<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>图书管理</title>
    <style>
        body {
            font-family: "Helvetica Neue", sans-serif;
            margin: 30px;
            background-color: #f9f9f9;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        form {
            margin-bottom: 20px;
            display: flex;
            gap: 10px;
            align-items: center;
            flex-wrap: wrap;
            justify-content: center;
        }

        label {
            font-size: 14px;
        }

        input[type="text"],
        input[type="number"] {
            padding: 5px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            padding: 6px 14px;
            font-size: 14px;
            cursor: pointer;
            border: none;
            border-radius: 4px;
            background-color: #2c3e50;
            color: white;
            transition: background-color 0.3s ease;
       /*这句代码可以实现鼠标悬停时背景颜色变化的过渡效果。*/
        }

        button:hover {
            background-color: #1a2733;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);/*给表格添加阴影*/
        }

        th, td {
            padding: 10px 12px;
            text-align: center;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #f0f0f0;
            color: #333;
        }

        tr:hover {
            background-color: #f5f5f5;
        }

        .action-btn {
            background-color: #2196F3;
            margin: 2px;
        }

        .action-btn:hover {
            background-color: #1976D2;
        }

        .delete-btn {
            background-color: #f44336;
        }

        .delete-btn:hover {
            background-color: #d32f2f;
        }

        .info {
            text-align: center;
            margin-top: 10px;
        }
    </style>
    <script>
        function deleteBook(id) {
            if (confirm("确定要删除该图书吗？")) {
                fetch(`/book/admin/${id}`, {
                    method: 'DELETE',
                    headers: {
                        'Authorization': `Bearer ${localStorage.getItem('token')}`
                        //功能：通过fetch发送 DELETE 请求到后端接口   /book/admin/{id}  。
                    }
                }).then(res => res.json())
                    .then(data => {
                        if (data.code === 0) {
                            alert("删除成功");
                            location.reload();
                        } else {
                            alert("删除失败：" + data.message);
                        }
                    });
            }
        }

        function editBook(id) {
            window.location.href = `/book/admin/edit/${id}`;

        }
    </script>
</head>
<body>
<h1>图书管理</h1>

<form action="/book/admin/books" method="get">
    <label>页码: <input type="number" name="pageNum" th:value="${pageNum}" min="1"></label>
    <label>每页条数: <input type="number" name="pageSize" th:value="${pageSize}" min="1"></label>
    <label>书名: <input type="text" name="title" th:value="${param.title}"></label>
    <button type="submit">查询</button>
    <a href="/book/admin/add?batch=0"><button type="button">新增图书</button></a>
    <a href="/book/admin/add?batch=1"><button type="button">批量新增图书</button></a>
</form>


<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>书名</th>
        <th>作者</th>
        <th>出版社</th>
        <th>ISBN</th>
        <th>状态</th>
        <th>创建时间</th>
        <th>更新时间</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="book : ${books}">
        <td th:text="${book.id}"></td>
        <td th:text="${book.title}"></td>
        <td th:text="${book.author}"></td>
        <td th:text="${book.publisher}"></td>
        <td th:text="${book.isbn}"></td>
        <td th:text="${book.status == 'AVAILABLE' ? '可借' : '已借'}"></td>
        <td th:text="${#temporals.format(book.createTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
        <td th:text="${#temporals.format(book.updateTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
        <td>
            <button class="action-btn" type="button" th:onclick="'editBook(' + ${book.id} + ')'">修改</button>
            <button class="delete-btn" type="button" th:onclick="'deleteBook(' + ${book.id} + ')'">删除</button>
        </td>
    </tr>
    <tr th:if="${#lists.isEmpty(books)}">
        <td colspan="9">暂无数据</td>
    </tr>
    </tbody>
</table>

<div class="info">
    <p>总记录数: <span th:text="${total}">0</span></p>
</div>
</body>
</html>